package com.hspedu.bot.service.impl.ranklist;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hspedu.bot.pojo.User;
import com.hspedu.bot.service.ranklist.GetRankListService;
import com.hspedu.bot.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GetRankListServiceIml implements GetRankListService {

    @Autowired
    private UserService userService;

    @Override
    public JSONObject getRankList(Integer page) {
        /**
         * page类传入参数为[起始页,展示数据条数]
         * */
        Page<User> userPage = new Page<>(page, 3);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("rating");
        List<User> users = userService.page(userPage, queryWrapper).getRecords();

        //返回数据
        JSONObject resp = new JSONObject();
        for (User user : users) {           //地址引用
            user.setPassword("");
        }
        resp.put("users", users);
        resp.put("users_total", userService.count(null));
        return resp;
    }
}
